#include <iostream>
#include "Texture.h"
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
Texture::Texture(const char *fileName, int format)
{
	//纹理
	glGenTextures(1,&m_id);
	glBindTexture(GL_TEXTURE_2D, m_id);
	//设置环绕和过滤方式
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	int width,height,nrChannels;
	unsigned char *data = stbi_load(fileName, &width, &height, &nrChannels, 0);
	if(data!= nullptr)
	{
		glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);//传送数据
		glGenerateMipmap(GL_TEXTURE_2D);//生成多级渐远
	}
	else
		std::cout<<"纹理文件加载失败";
	stbi_image_free(data);
}

Texture::~Texture()
{
	glDeleteTextures(1,&m_id);
}

const GLuint Texture::getID() const
{
	return m_id;
}

